package com.test.A31;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class WebConfig {
    @ControllerAdvice
    static class MyControllerAdvice {
    	
    	/***
    	 * 设置全局异常处理
    	 * @param e
    	 * @return
    	 */
        @ExceptionHandler
        @ResponseBody
        public Map<String, Object> handle(Exception e) {
        	Map map =new HashMap();
        	map.put("error", e.getMessage());
            return map;
        }
    }

    /****
     * 创建ExceptionHandlerExceptionResolver对象
     * @return
     */
    @Bean
    public ExceptionHandlerExceptionResolver resolver() {
        ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
        resolver.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
        return resolver;
    }
}
